//index.js
//获取应用实例
import S_request from '../../utils/requestService.js';
import CONFIG from '../../config.js';
const app = getApp()
let formId = 0;//表单Id
Page({
  data: {
    gameList: [],
    isShowTask: false,
    modalObj: {},
    isShowNetWork: false,
    id: '',
  },
  onShareAppMessage: function(res) {
    wx.showShareMenu({
      withShareTicket: true,
      success: function() {}
    });
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    let obj = { 'pagepath': CONFIG.SHARE.path }
    S_request.index.recordShare("-1", obj);
    return {
      title: CONFIG.SHARE.title,
      path: CONFIG.SHARE.path,
      imageUrl: CONFIG.SHARE.imageUrl
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let share = options.share;
    if (share == 1) {
      console.log("游戏界面转发")
    }
    this.getPlayGameList();
  },
  getPlayGameList: function() {
    let self = this;
    S_request.play.getPlayGameList((data) => {
      // if (goodsData.statusCode == CONFIG.CODE.REQUESTERROR) {
      //   this.setData({
      //     "toast.hidden": false,
      //     "toast.icon": "clear",
      //     "toast.msg": "请求超时",
      //     "loading.hidden": true
      //   });
      //   return;
      // }
      // console.log("data====" + JSON.stringify(data));
      let lists = data.lists;
      app.globalData.playLists = lists[0].list;
      self.setData({
        gameList: lists[0].list
      });
    }, this)
  },
  playGame: function(e) {
    let id = e.currentTarget.dataset.id;
    console.log("id====" + id);
    let param = 'formId=' + formId
    S_request.index.tongJiNum(id, param, (result) => {
      console.log("data====" + result);
    })
    let path = e.currentTarget.dataset.path;
    if (path != undefined && path != '') {
      wx.navigateTo({
        url: path,
      })
    }
  },
  openDetail: function(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: '../detail/detail?gameId=' + id + "&type=play",
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getPlayGameList();
    this.showTaskDialog();
  },
  showTaskDialog: function() {
    if (app.globalData.taskNum == 0) {
      this.setData({
        modalDealIdx: 0,
        isShowTask: false
      });
    } else if (this.data.isShowTask != true) {
      let animation1 = wx.createAnimation({
        transformOrigin: "50% 50%",
        duration: 800,
        timingFunction: "ease",
      });
      animation1.scale(0.4).step();
      this.data.modalObj.animation = animation1.export();
      this.data.modalObj.taskNum = app.globalData.taskNum;
      this.setData({
        modalDealIdx: 1,
        isShowTask: true,
        modalObj: this.data.modalObj
      });

      setTimeout(function() {
        animation1.scale(1).step()
        this.data.modalObj.animation = animation1.export();
        this.setData({
          modalObj: this.data.modalObj
        })
      }.bind(this), 500)
    } else {
      this.data.modalObj.taskNum = app.globalData.taskNum;
      this.setData({
        modalObj: this.data.modalObj
      });
    }
  },
  getNetworkStatus: function() {
    let self = this;
    //无网络时做提示no net
    wx.onNetworkStatusChange(function(res) {
      if (res.networkType == "none") {
        self.setData({
          isShowNetWork: true
        })
      } else {
        self.setData({
          isShowNetWork: false
        })
        self.getPlayGameList();
        self.showTaskDialog();
      }
    })
  },
  againLoadAc: function() {
    let self = this;
    wx.showLoading({
      title: '',
    })

    setTimeout(function() {
      wx.hideLoading()
      self.getPlayGameList();
      self.showTaskDialog();
    }, 1000)
  },
  //触摸变小动画
  setShrink: function(e) {
    this.setData({
      id: e.currentTarget.dataset.id,
    });
    let animation1 = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 200,
      timingFunction: "ease"
    });
    animation1.scale(0.95).step();
    this.setData({
      animation1: animation1.export()
    });
  },
  //手指离开放大动画
  remShrink: function(e) {
    this.setData({
      id: e.currentTarget.dataset.id,
    });
    let animation1 = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 200,
      timingFunction: "ease"
    });
    animation1.scale(1.0).step();
    this.setData({
      animation1: animation1.export()
    });
  },
  formSubmit: function (e) {
    formId = e.detail.formId;
    console.log('form发生了submit事件，携带数据为：', e.detail.value + ", " + e.detail.formId)
  }
})